package com.jxs.android.ges;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jxs.android.util.HandlerHolder;
import com.jxs.android.util.MemoryUtil;
import com.jxs.android.util.StringUtils;
import com.jxs.android.util.Timer;
import com.jxs.android.weiget.BuAlertDailog;
import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.TopBar;
import com.jxs.android.weiget.WheelDateDialog;

@SuppressLint("HandlerLeak")
public class Visit_Records_NewCreate extends AbstractActivity {
	// 2.4.6 getSmsContent 【获取发送页面内容】
	private BuLoadingDailog progressDialog;
	private Context context;
	private TextView staff_name, industry_type_name, cust_name, create_date, update_date;
	private EditText basic;
	private CheckBox way_phone, way_site;

	private LinearLayout cust_name_ll;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		context = this;
		initTopBar(savedInstanceState, R.layout.bu_visit_records_detail);
		initUI();
	}

	private void initUI() {
		staff_name = (TextView) findViewById(R.id.staff_name);
		industry_type_name = (TextView) findViewById(R.id.industry_type_name);
		cust_name = (TextView) findViewById(R.id.cust_name);
		create_date = (TextView) findViewById(R.id.create_date);
		update_date = (TextView) findViewById(R.id.update_date);
		cust_name_ll = (LinearLayout) findViewById(R.id.cust_name_ll);
		basic = (EditText) findViewById(R.id.basic);
		way_phone = (CheckBox) findViewById(R.id.way_phone);
		way_site = (CheckBox) findViewById(R.id.way_site);

		findViewById(R.id.cust_img).setVisibility(View.VISIBLE);

		cust_name.setText("");
		staff_name.setText(GesApplication.getUserInfo().getName());
		industry_type_name.setText(GesApplication.getUserInfo().getIndustry_type_name());
		create_date.setText(Timer.getSDFyyyyMMdd().format(System.currentTimeMillis()));
		update_date.setText(create_date.getText());
		cust_name_ll.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(context, GetCustomerList.class), REQ_CST_LIST);
			}
		});
		create_date.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showDateDialog();
			}
		});
	}

	private void showDateDialog() {
		WheelDateDialog dateDialog = new WheelDateDialog(this, 0x8787, create_date.getText().toString());
		dateDialog.setDateChangeListener(new WheelDateDialog.DateChangeListener() {

			@Override
			public void onKeySave(int requestCode, String dateTimeValue) {
				create_date.setText(dateTimeValue);
				update_date.setText(create_date.getText());
			}
		});
		dateDialog.show();
	}

	final int REQ_CST_LIST = 0xc001;

	private String cust_id = "";

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (REQ_CST_LIST == requestCode && resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			cust_id = bundle.getString("id");
			cust_name.setText(bundle.getString("content"));
		}
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		super.topBarReturn(topBar);
		topBar.setLogicBtnText("保存");
		topBar.setTitle("新建走访记录");
		topBar.showLogicBtn();
		topBar.hindFilter();
		topBar.hindSpinnerBtn();
		progressDialog = new BuLoadingDailog(this);
		topBar.setLogicBtnListern(new TopBar.LogicBtnListern() {
			@Override
			public void onClickLogicBtn() {
				if (StringUtils.isEmpety(cust_id)) {
					errorMsgPop("请选择客户！");
					return;
				}
				if (StringUtils.isEmpety(basic.getText().toString())) {
					errorMsgPop("请描述拜访基本情况！");
					return;
				}
				if (!way_phone.isChecked() && !way_site.isChecked()) {
					errorMsgPop("请选择拜访方式！");
					return;
				}

				progressDialog.showLoading(R.string.load_and_wait);
				new Visit_Records_SaveHolder(handler, "").saveVisitsRecordX(//
						GesApplication.getUserInfo().getTicket(), //
						create_date.getText().toString(),//
						cust_id, //
						cust_name.getText().toString(),//
						basic.getText().toString(),//
						way_phone.isChecked(), way_site.isChecked());
			}
		});
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (!MemoryUtil.isCurrentActivity(Visit_Records_NewCreate.class)) {
				return;
			}

			if (null != progressDialog && progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
			if (msg.what == HandlerHolder.What.ERROR_MSG) {
				errorMsgPop(msg.obj.toString());
			} else if (msg.what == HandlerHolder.What.TO_LOGIN) {
				errorMsgToLogin(msg.obj.toString());
			} else if (msg.what == HandlerHolder.What.VISIT_RECORDS_SAVE_RESLT_OK) {
				errorMsgPop("创建成功！", new BuAlertDailog.IPhoneAlertDailog() {

					@Override
					public String getOkText(Context context) {
						return getString(R.string.promp_confirm);
					}

					@Override
					public void okLogic(Dialog dialog) {
						dialog.cancel();
						setResult(RESULT_OK);
						finish();
					}
				});
			}
		}
	};

}
